﻿using System;

namespace RexToy.UI.Controls
{
    public enum ButtonStatus
    {
        [EnumValue("none")]
        [ImportMember("None")]
        None,

        [EnumValue("up")]
        [ImportMember("Up")]
        Up,

        [EnumValue("down")]
        [ImportMember("Down")]
        Down,

        [EnumValue("hover")]
        [ImportMember("Hover")]
        Hover
    }

    public class ButtonBase : Control
    {
        

        protected bool __enable = true;
        public virtual bool Enable
        {
            get { return __enable; }
            set
            {
                if (value)
                    this.__domNode.RemoveClass("ui-disabled");
                else
                    this.__domNode.AppendClass("ui-disabled");
                this.SetButtonClass(ButtonStatus.Up);
                this.__enable = value;
            }
        }

        protected string __text = string.Empty;
        public virtual string Text
        {

            get { return __text; }
            set
            {
                this.__text = value;
                var dom = this.__domNode.SearchByName("content");
                dom._element.InnerHTML = value;
            }
        }

        public override Palette Palette
        {
            get
            {
                return this.__palette;
            }
            set
            {
                this.SetButtonClass(ButtonStatus.None);
                this.__palette = value;
                this.SetButtonClass(ButtonStatus.Up);
            }
        }

        public override bool Standalone
        {
            get
            {
                return __standalone;
            }
            set
            {
                __standalone = value;
                var dom = this.__domNode.SearchByName("spn");
                if (__standalone)
                {
                    this.__domNode.AppendClass("ui-btn-corner-all");
                    this.__domNode.AppendClass("ui-shadow");
                    dom.AppendClass("ui-btn-corner-all");
                }
                else
                {
                    this.__domNode.RemoveClass("ui-btn-corner-all");
                    this.__domNode.RemoveClass("ui-shadow");
                    dom.RemoveClass("ui-btn-corner-all");
                }
            }
        }

        private string[] events = new string[] { "onclick" };

        protected ButtonBase(string text)
        {
            this.__palette = Palette.B;
            this.__text = text;
            this.__domNode = RexToy.UI.Controls.DomNode.Create(new DomNodeCreateOptions()
            {
                tag = Tag.A,
                className = "ui-btn ui-shadow ui-btn-corner-all",
                items = new DomNodeCreateOptions[]{new DomNodeCreateOptions()
                    {
                        tag = Tag.Span,
                        hashName = "spn",
                        className = "ui-btn-inner ui-btn-corner-all",
                        items = new DomNodeCreateOptions[] {new DomNodeCreateOptions()
                            {
                                tag = Tag.Span,
                                hashName = "content",
                                className = "ui-btn-text",
                                innerHTML = this.__text
                            }
                        }
                    }
                }
            });


            this.__domNode.AttachEvent(DomEvent.OnMouseOut, new Action(this.mouseup).Bind(this));
            this.__domNode.AttachEvent(DomEvent.OnMouseDown, new Action(this.mousedown).Bind(this));
            this.__domNode.AttachEvent(DomEvent.OnMouseOver, new Action(this.mouseover).Bind(this));
            this.__domNode.AttachEvent(DomEvent.OnMouseUp, new Action(this.mouseup).Bind(this));
            this.__domNode.AttachEvent(DomEvent.OnClick, new Action(this._onclick).Bind(this));
            this.SetButtonClass(ButtonStatus.Up);
        }

        protected void SetButtonClass(ButtonStatus status)
        {
            if (this.__enable)
            {
                ButtonStyleHelper.SetDomNodeClass(this.__domNode, status, this.__palette);
            }
        }

        private void mouseup()
        {
            this.SetButtonClass(ButtonStatus.Up);
        }

        private void mousedown()
        {
            this.SetButtonClass(ButtonStatus.Down);
        }

        private void mouseover()
        {
            this.SetButtonClass(ButtonStatus.Hover);
        }

        private void _onclick()
        {
            if (this.__enable)
            {
                this.__FireEvent("onclick");
            }
        }
    }
}
